/*
 * Copyright (c) 2013   Tobias Welther
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package de.tudarmstadt.kom.androidsensorapi.sensor.listener.location;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

import java.util.Date;

import de.tudarmstadt.kom.androidsensorapi.datarepresentation.location.LocationProvider;
import de.tudarmstadt.kom.androidsensorapi.datarepresentation.location.SimpleLocation;
import de.tudarmstadt.kom.androidsensorapi.sensor.data.concrete.LocationSensorData;
import de.tudarmstadt.kom.androidsensorapi.sensor.listener.AbstractSensor;
import de.tudarmstadt.kom.androidsensorapi.sensormanagement.SensorTypeToData;

/**
 * Created by Tobias Welther
 */
public class LocationSensor extends AbstractSensor<LocationSensorData>
        implements LocationListener {


    /**
     * Creates a LocationSensor to poll data from
     */
    public LocationSensor() {
        super(SensorTypeToData.Location);
    }


    @Override
    public void onLocationChanged(Location location) {
        Location internLocation = location;
        SimpleLocation simpleLocation = new SimpleLocation(internLocation);

        String locationProviderString = internLocation.getProvider();
        LocationProvider provider = LocationProvider.resolve(locationProviderString);
        Date currentDate = new Date();

        lastData = new LocationSensorData(currentDate, simpleLocation, provider);
        //TODO call updates here when possible
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        //Not yet important
    }

    @Override
    public void onProviderEnabled(String provider) {
        //Not yet important
    }

    @Override
    public void onProviderDisabled(String provider) {
        //Not yet important
    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        //Not needed.
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        //Not needed
    }

    @Override
    public void unregister(SensorManager manager) {
        if(!isRegistered()) return;
        //TODO


        this.isRegistered = false;
    }

    @Override
    public void register(SensorManager manager) {
        if(isRegistered()) return;
        //TODO

        this.isRegistered = true;
    }
}
